clear
set more      off
set varabbrev off
set gr        off
set linesize 255

/* INPUTS */

foreach num of numlist 1998(1)2019 {
	local indata_`num'      "/data/rawdata/cleaned/jobb_`num'"
}

local in_ui 		"/data2/cedsei/data2/search_ui"

/* OUTPUTS */ 

local out_uipop 	"/data2/cedsei/data2/ui_pop"
local out_wages 	"/data2/cedsei/data2/wages"
local out_rams 		"/data2/cedsei/data2/monthly_ee_panel_rkd"

*cd "/data2/cedsei/data2"

********************************************************************************
	
clear 
use `in_ui'

{ // Checking years:
gen temp = year(dofm(date))
gen temp2 = !mi(search_ext)
tab temp temp2 
drop temp* 
// Search data stretch 2005-2018. UI data covers 1999-2018 (not seen here, they are removed after merging in the A2-file. 
// Rams will cover the union of them. 
}

gen year = year(dofm(date))
keep lopnr year 
duplicates drop 
expand 2, gen(newvar)
replace year = year + 1 if newvar==1 
drop newvar 
duplicates drop 
save `out_uipop', replace 

/* WAGES */ 
foreach num of numlist 1999(1)2018 {
	di "Year: `num'"
	qui {

	 if `num'<2009 {
		clear 
		use LopNr peorglopnr Manl using /data/rawdata/SCB/SCB_Leverans_20221117/DS_lev_Loner_Tjm_`num'
		rename Manl manl 

	}
	else {
		clear 
		use LopNr peorglopnr MLonG using /data/rawdata/SCB/SCB_Leverans_20221117/DS_lev_Loner_Tjm_`num'
		rename MLonG manl 
	}
	rename LopNr lopnr 
	rename peorglopnr lopnr_peorgnr 
	duplicates drop 
	gen sekt = "tjm"
	tempfile tjm
	save 	`tjm', replace

	if `num'<2006 {
		clear 
		use LopNr peorglopnr manl using /data/rawdata/SCB/SCB_Leverans_20221117/DS_lev_Loner_Stat_`num'
	}
	else {
		clear 
		use LopNr peorglopnr grlon using /data/rawdata/SCB/SCB_Leverans_20221117/DS_lev_Loner_Stat_`num'
		rename grlon manl 
	}

	rename LopNr lopnr 
	rename peorglopnr lopnr_peorgnr
	duplicates drop 
	gen sekt = "stat"
	tempfile stat
	save 	`stat', replace

	if `num'<2006 {
		clear 
		use LopNr peorglopnr manl using /data/rawdata/SCB/SCB_Leverans_20221117/DS_lev_Loner_Landsting_`num'
	}
	else {
		clear 
		use LopNr peorglopnr grlon using /data/rawdata/SCB/SCB_Leverans_20221117/DS_lev_Loner_Landsting_`num'
		rename grlon manl 
	}
	rename LopNr lopnr 
	rename peorglopnr lopnr_peorgnr
	duplicates drop 
	gen sekt = "lnd"
	tempfile lnd
	save 	`lnd', replace

	if `num'<2006 {
		clear 
		use LopNr peorglopnr manl using /data/rawdata/SCB/SCB_Leverans_20221117/DS_lev_Loner_Kommun_`num'
	}
	else {
		clear 
		use LopNr peorglopnr grlon using /data/rawdata/SCB/SCB_Leverans_20221117/DS_lev_Loner_Kommun_`num'
		rename grlon manl 
	}
	rename LopNr lopnr 
	rename peorglopnr lopnr_peorgnr
	duplicates drop 
	gen sekt = "kom"
	tempfile kom
	save 	`kom', replace


	 if `num'<2009 {
		clear 
		use LopNr peorglopnr Manl using /data/rawdata/SCB/SCB_Leverans_20221117/DS_lev_Loner_Arb_`num'
		rename Manl manl 

	}
	else {
		clear 
		use LopNr peorglopnr MLonG using /data/rawdata/SCB/SCB_Leverans_20221117/DS_lev_Loner_Arb_`num'
		rename MLonG manl 
	}
	rename LopNr lopnr 
	rename peorglopnr lopnr_peorgnr 
	duplicates drop 
	gen sekt = "arb"

	append using `tjm'
	replace sekt = "tjm" if sekt==""
	append using `kom'
	replace sekt = "kom" if sekt==""
	append using `lnd'
	replace sekt = "lnd" if sekt==""
	append using `stat'
	replace sekt = "stat" if sekt==""
	gen year = `num'
	bys lopnr lopnr_peorgnr: keep if _n==1
	if `num'>1999 {
		append using `out_wages' 
	}

	save `out_wages', replace 
	}
}

foreach num of numlist 1999(1)2017 {
	clear
	use lopnr lopnr_peorgnr year lonfink manfran mantill yrkstallnku using `indata_`num''
	merge m:1 lopnr year using `out_uipop', keep(3) nogen 
	
	di "Year: `num'"
	qui {
	keep if yrkstallnku=="2"
	drop yrkstallnku
	gcollapse (sum) lonfink (min) manfran (max) mantill, by(lopnr lopnr_peorgnr)
	gen imputed = cond(manfran==0,1,cond(mantill==0,1,0))
	replace manfran = 1  if manfran==0
	replace mantill = 12 if mantill ==0
	foreach var of varlist lonfink manfran mantill lopnr_peorgnr imputed {
		replace `var' = 0 if mi(`var')
	}

	expand 12 
	
	bys lopnr lopnr_peorgnr: gen month = _n
	assert inrange(month,1,12)
	gen year = `num'
	gen date = ym(year, month)
	format date %tm
	
	gen earnings_month = cond(inrange(month,manfran,mantill),lonfink / (mantill - manfran + 1),0)
	gen working 	   = earnings_month > 0
	
	merge m:1 lopnr lopnr_peorgnr year using `out_wages' 
	drop if _merge==2 
	drop _merge 
	replace manl = . if working == 0 & !mi(manl)
	replace manl = . if manl == 0    & !mi(manl)
	gen private = cond(sekt!="", cond(sekt=="tjm",1,cond(sekt=="arb",1,0)),.)
	keep lopnr date working earnings_month manl private
	gcollapse (sum) earnings_month (max) working manl private, by(lopnr date)

	if `num'>1999 {
	  append using `out_rams'
	}
	save  `out_rams', replace
	}	
}


